/*******************************************************
*  Team name: FTPMP
*  Class involve: CardController , UseMTController, DrawMTController, Board, RoundWindow, Map, Player
*  Version, Author, History of modification: 
*  Ver 1.0, Fred,
*  -add setRoundWindow
*  
*  
*  Ver 1.1, Fred,
*  -add SACard Controller, eventCard Controller
*  
*  Ver 1.2, Ada,
*  -add getEventBoxNo, getSABoxNo, getMTBoxNo
*  -add getWinMeth 
*******************************************************/

package control;
import entity.Board;
import entity.Player;
import entity.Card;
import entity.Map;
import java.awt.*;
import javax.swing.*;

import boundary.BoardWindow;
import boundary.RoundWindow;
import java.util.Vector;



public class BoardEngine 
{
	
	Player player;
	int playerNo;
	int currentPlayer;
	String []currentMTSet;
	int winCon;
	int winMeth;
	int winningPlayer;
	String winMsg;
	String atPanel;
	Map map;
	CardController eventCardController;
	CardController SACardController;
	UseMTController useMTController;
	DrawMTController drawMTController;
	
	private Board board;
	RoundWindow roundWindow;
	BoardWindow boardWindow;
	
	/*------------------- round action ------------------------*/
	//display board + game status + card page
	//check if magic card is used on this player
	//prompt users to throw a dice
	//move user's chess 
	//check what action to be performed (check board value)
	//if event/ magic tools/ say&act
	//open new window(call func)
	//use magic tool (no loop, use only once)
	//check winning condition
	//loop to another player
		

	public BoardEngine() 
	{

		this.setBoard(new Board());
		this.playerNo=0;
		this.winCon=0;
		this.atPanel="Main";
		this.map=this.getBoard().getMap();
	}
	
	public void initActionWindow(RoundWindow RW)
	{
		this.roundWindow=RW;
		this.boardWindow=this.roundWindow.getBoardWindow();
		
	}
	public void setPlayerNo(int playerNo)
	{
		this.playerNo=playerNo;
		System.out.println("This is boardEngine: "+this.playerNo);
		this.getBoard().initPlayerList(playerNo);
	}
	
	public int[] getBoxNo(String type)
	{
		if(type=="Event")
			return map.getEventBoxList();
		else if(type=="SA")
			return map.getSABoxList();
		else
			return map.getMTBoxList();
	}
	public int getWinMeth()
	{
		return this.winMeth;
	}
	public void initRoundWindow()
	{
		this.roundWindow.setPlayerNo(playerNo);
		this.roundWindow.setPlayerPos();
		this.boardWindow.initPlayerPos(playerNo);
		this.roundWindow.printBoardPanel();
		this.roundWindow.printStatusPanel();
		this.roundWindow.printMTPanel();
	}
	public void initCardController(RoundWindow rw)
	{
		this.eventCardController=new EventCardController(this,rw);
		this.SACardController=new SACardController(this,rw);
		this.useMTController=new UseMTController(this,rw);
		this.drawMTController=new DrawMTController(this,rw);
	}
	public void getCard(String type,int cardNo) 
	{	
		System.out.println("HI! Player "+ (this.currentPlayer+1));
		if(type=="Event")
			this.eventCardController.drawCard(this.getBoard().getCard(type,cardNo),this.currentPlayer);
		else if(type=="SA")
			this.SACardController.drawCard(this.getBoard().getCard(type,cardNo),this.currentPlayer);
	}
	
	public boolean validUserMtType(int pid, String MTType)
	{
		//boolean yes=false;
		System.out.println("Hello! Player "+(pid+1));
		currentMTSet= getBoard().getPlayerMT(pid);
		for(int i=0; i<currentMTSet.length; i++) 
		{
			//System.out.println("Hey !: "+mt[i]);
			if(currentMTSet[i].compareTo(MTType)==0) 
				return true;
			
		}
		
		return false;
	}
	

	//pending for refactoring
	/*public void setReward(int pid, int eid) 
	{
		this.board.giveSAReward(pid, eid);
	}*/
	
	public void assignRole(String[]roleList)
	{
		this.getBoard().assignRole(roleList);
	}
	public int getPlayerNo() 
	{
		return this.playerNo;
	}
	public void setWinCon(int winCon) 
	{
		this.winCon=winCon;
		this.getBoard().setWinCon(this.winCon);
	}
	
	public EventCardController getEventCardController()
	{
		return (EventCardController) this.eventCardController;
	}
	public SACardController getSACardController()
	{
		return (SACardController) this.SACardController;
	}
	public UseMTController getUseMTController()
	{
		return this.useMTController;
	}
	public DrawMTController getDrawMTController()
	{
		return this.drawMTController;
	} 
	
	public int getCurrentPlayer() 
	{
		return this.currentPlayer;
	}
	public int getWinningPlayer() 
	{
		return this.winningPlayer;
	}
	public String getWinningMsg()
	{
		return this.winMsg;
	}
	public String getMagicToolDesp(int currentPlayer, int MTID)
	{	
		String desp = getBoard().getMagicToolDesp(currentPlayer, MTID);
		return desp;
	}
	public String[] getCurrentPlayerMT()
	{
		return this.getBoard().getPlayerMT(this.currentPlayer);
	}
	public void startRound()
	{

		if(this.currentPlayer>=playerNo)
			this.currentPlayer=0;
		
		int step=this.boardWindow.rollDice();
		this.roundWindow.finishRollDice();
		this.currentMTSet=getBoard().getPlayerMT(this.currentPlayer);
		
		int boxNo=this.roundWindow.movePlayer(this.currentPlayer, step);
		this.roundWindow.printMTPanel();
		this.roundWindow.printBoardPanel();
		this.varifyBoxType(boxNo);	
	}
	private void varifyBoxType(int boxNo)
	{
		String boxType=getBoard().validateBoxType(boxNo);
		
		System.out.println("BoardEngine: "+boxType);
		if(boxType!="Event" && boxType!="SA")
		{
			this.roundWindow.enableEndTurn(true);
			this.atPanel="Main";
		}
		else
		{
			this.setEventSAMTVisible();
			this.atPanel=boxType;
			this.getCard(boxType, 0);
			this.roundWindow.enableEndTurn(false);
		}
	}
	
	public int getWinCon()
	{
		return this.winCon;
	}
	
	public String getAtPanel()
	{
		return this.atPanel;
	}
	
	public String setAtPanel(String atPanel)
	{
		return this.atPanel=atPanel;
	}

	public void setEventSAMTVisible()
	{
		for(int i=0;i<this.currentMTSet.length;i++)
		{
			if(this.currentMTSet[i]=="Skip")
			{
				this.roundWindow.setMTEnabled(i,true);
			}
			else if(this.currentMTSet[i]=="Forward")
			{
				this.roundWindow.setMTEnabled(i,false);
			}
		}
	}

	public void switchNextPlayer()
	{
		this.currentPlayer++;
		if(this.currentPlayer==this.playerNo)
			this.currentPlayer=0;
	}
	public void setPlayerMT() 
	{
		for(int i=0;i<this.playerNo;i++)
		{
			for(int j=0;j<3;j++)
			{
				getBoard().drawPlayerMT(i);
			}
			System.out.println(" "+(i+1)+" MagicToolNo: "+getBoard().getPlayerMTNo(i));
		}
	}
	
	public void drawPlayerMT(int PID)
	{
		this.getBoard().drawPlayerMT(PID);
	}
	
	
	public void removeUsedMT(int PID, int MTID)
	{
		this.getBoard().removeUsedMT(PID,MTID);
	}
	public void setWinMeth(int winMeth) 
	{
		this.winMeth=winMeth;
	}

	public int[] printSE()
	{
		return getBoard().getPlayerSE();
	}
	
	public boolean fulfillWinCon()
	{
		if(this.winMeth==1)
		{
			if(this.board.validateTotalSE())
			{
				this.winMsg=" Congratulation! All of you won the game. ";
				return true;
			}
			else
				return false;
		}
		else
		{
			if(this.board.validateWinCon()!=-1)
			{
				this.winningPlayer=this.getBoard().validateWinCon()+1;
				this.winMsg=" Congratulation! Player "+this.winningPlayer+" won the game. ";
				return true;
			}
			return false;
		}
	}
	public void addBonus() 
	{
		System.out.println("BoardEngine  - add Bonus - Player: "+this.currentPlayer+1);
		this.getBoard().addCurrentBonus(this.currentPlayer);	
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public Board getBoard() {
		return board;
	}

}
